package com.haoran.dwz.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.haoran.dwz.domain.entities.Group;
import com.haoran.dwz.domain.pojo.GroupVO4Add;
import com.haoran.dwz.domain.pojo.GroupVO4Query;
import com.haoran.dwz.domain.pojo.GroupVO4Update;
import com.haoran.dwz.rest.R;
import com.haoran.dwz.service.GroupService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = { "Groups" }, description = "分组管理",position=10)
@RestController
@RequestMapping(value = "/groups", headers = "Authorization",produces = "application/json;charset=UTF-8")
public class GroupController {
	
	@Autowired
	private GroupService groupService;
	
	@ApiOperation("查询所有分组列表")
	@RequestMapping(method = RequestMethod.GET)
	public R list() {
		try {
			return R.success(groupService.treeGroups());
		} catch (Exception e) {
			return R.failure(e);
		}
	}
	
	@ApiOperation("分页查询分组列表")
	@RequestMapping(value = "/page", method = { RequestMethod.GET })
	public Page<Group> findByPageable(GroupVO4Query group) {
		Sort sort = new Sort(Direction.ASC, "number");
		Pageable pageable = new PageRequest(group.getOffset()/group.getLimit(), group.getLimit(), sort);
		
		Page<Group> entryByPageable = groupService.findByPageable(pageable);
		return entryByPageable;
	}
	
	@ApiOperation("编辑分组")
	@RequestMapping(method = RequestMethod.PUT)
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "header", required=true, name = "Authorization", value = "登录成功返回的token") })
	public R update(@RequestBody GroupVO4Update group) {
		try {
			return R.success(groupService.update(group));
		} catch (Exception e) {
			return R.failure(e);
		}
	}
	
	@ApiOperation("新增分组")
	@RequestMapping(method = RequestMethod.POST)
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "header", required=true, name = "Authorization", value = "登录成功返回的token") })
	public R add(@RequestBody GroupVO4Add groupVO) {
		try {
			return R.success(groupService.newAdd(groupVO));
		} catch (Exception e) {
			return R.failure(e);
		}
	}
	
	@ApiOperation("删除分组")
	@RequestMapping(path="{id}",method = RequestMethod.DELETE)
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "header", required=true, name = "Authorization", value = "登录成功返回的token"),
			})
	public R delete(@PathVariable String id,@ApiParam("是否包含下级，默认不包含")@RequestParam(defaultValue="false",required=false) boolean subInclude) {
		try {
			groupService.delete(id,subInclude);
			return R.success("删除成功");
		} catch (Exception e) {
			return R.failure(e);
		}
	}
	
	@ApiOperation("批量删除分组")
	@RequestMapping(path="batchDel",method = RequestMethod.DELETE)
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "header", required=true, name = "Authorization", value = "登录成功返回的token"),
			})
	public R delete(@ApiParam(example="4028f8815f944774015f944894b10002,4028f8815f944774015f944894b10004",value="主键数组") @RequestParam(name="ids[]") String[] ids,@ApiParam("是否包含下级，默认不包含")@RequestParam(defaultValue="false",required=false) boolean subInclude) {
		try {
			if(ids != null) {
				groupService.delete(ids,subInclude);
			}
			return R.success("删除成功");
		} catch (Exception e) {
			return R.failure(e);
		}
	}
}
